
// <copyright file="PoolItem.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The pool item.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Pooling
{
    using System;

    /// <summary>
    /// The pool item.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class PoolItem<T> : AbstractDisposable, IPoolItem<T>
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private T adaptedObject;

        /// <summary>
        /// The callback.
        /// </summary>
        private ReturnAdaptedObjectToPoolDelegate<T> _callback;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the adaptedObject protected.
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// </exception>
        protected T AdaptedObjectProtected
        {
            get
            {
                if (!this.IsDisposed)
                {
                    return this.adaptedObject;
                }

                throw new ObjectDisposedException("this", "The object is already disposed and the AdaptedObjectProtected is returned to the Pool");
            }
        }

        #endregion Properties

        #region Explicit Interface Methods

        /// <summary>
        /// The set adaptedObject and callback.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        /// <param name="callback">
        /// The callback.
        /// </param>
        void IPoolItem<T>.SetAdaptedObjectAndCallback(T adaptedObject, ReturnAdaptedObjectToPoolDelegate<T> callback)
        {
            this.adaptedObject = adaptedObject;
            this._callback = callback;
        }

        #endregion Explicit Interface Methods

        #region Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            this._callback(this, this.adaptedObject);
        }

        #endregion Methods
    }
}
